package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;
/**
 * http://www.ifeng.com/ 凤凰网
 * @author allen
 */
public class IfengNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		if(url.indexOf("/search/")!=-1)
			return false;
		String[] regex={
				"http://.*.ifeng.com/.*/[0-9]+/[0-9]{5,}.shtml",
				"http://.*.ifeng.com/.*/[0-9]{6}/.*[0-9]+.shtml",
				"http://.*.ifeng.com/.*/.*_0.shtml",
				"http://.*.finance.ifeng.com/.*/[0-9]+/[0-9]+",
				"http://.*.finance.ifeng.com/.*/[a-z]*.php\\?id=[0-9]+&symbol=[0-9]+"
				};
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		doc.select(".relateNews,#cmtBox,div.right,.box_share,a.Ablack,div.pageContainer,strong:containsOwn(相关阅读：)").remove();
		
		/*
		 * 标题解析
		 */
		title = doc.select("div.tilte h2").text();
		if(StringUtils.isBlank(title))
		title = doc.select("h1#artical_topic").text();
		if (StringUtils.isBlank(title))
			title = doc.select("h1").text();
		if (StringUtils.isBlank(title))
			title = doc.select("div.tit").text();
		if (StringUtils.isBlank(title))
			title = doc.select("div.arl-cont h3").text();
		if (StringUtils.isBlank(title))
			title = doc.select("div.title h2").text();
		if (StringUtils.isBlank(title))
			title = doc.select("#artical_topic").text();
     

		/*
		 * 日期解析
		 */
		date = DateUtils.matchDate(doc.select("div#artical_sth").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.title_1").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.artInfo").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.title").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.date").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("span.ai-date").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.slidePicGq div[align=right]").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.titL").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("h2").text());
		if (date == null)
			date = DateUtils.matchDate(doc.select("div.zuo_word").text());
		if (date == null)
			date = DateUtils.matchDate(url);

		doc.select(":containsOwn(免责声明：本文仅代表作者个人观点，与凤凰网无关),#VideoPlayerDiv").remove();
		content = HtmlCleaner.getContentHtml(url, doc.select("div#main_content"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div#artical_real"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div.arl-c-txt"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("#photoDesc"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("dd#imgMemoBox"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div.gonggao"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("p.photoDesc"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div.article"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div.zhs_mid_02"));
		if (StringUtils.isBlank(content))
			content = HtmlCleaner.getContentHtml(url, doc.select("div.photo-info-text"));

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

	  String author=JSoupUtils.matchAuthor(doc, "来源：");
	  news.setAuthor(author);
	  
	  return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		IfengNewsAnalyse a=new IfengNewsAnalyse();
		String url="http://hz.house.ifeng.com/detail/2016_11_08/50928908_0.shtml";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate(){
		return true;
	}
}
